package com.itheima.buffer;

import com.itheima.domain.Student;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//请编写main()方法，定义一个字符缓冲输入流BufferedReader对象，读取”test3_4.txt”文件，
// 一次读取一行，将每行数据封装为一个Student对象，并将Student对象存储到一个集合。遍历并打印集合的所有Student信息。
//		//定义一个BufferedReader对象
//		BufferedReader in = new BufferedReader(......);
//		//定义一个集合
//		List<Student> stuList = new ArrayList<>();
//		//一次读取一行
public class Test4 {
    public static void main(String[] args) throws IOException {
        BufferedReader bw = new BufferedReader(new FileReader("test3_4.txt"));
        List<Student> stuList = new ArrayList<>();
        String line;
        while ((line = bw.readLine()) != null) {
            //System.out.println(line);
            method(line,stuList);
           // Student s1 = new Student(line);
          //  System.out.println(s1);
            //stuList.add(s1);
        }
        bw.close();
        for (Student student : stuList) {
            System.out.println(student);
        }

    }

    private static void method(String line,List<Student> stuList) {
        int name = line.indexOf("name='");
        int gender=line.indexOf("', gender='");
        int age=line.indexOf("', age=");
        int mark=line.indexOf(", mark=");
        int end=line.indexOf("}");
        String nameStr = line.substring(name + 6, gender);
        String genderStr = line.substring(gender + 10, age);
        String ageStr = line.substring(age + 7, mark);
        String markStr = line.substring(mark + 7, end);
        int ageInt = Integer.parseInt(ageStr);
        int markInt = Integer.parseInt(markStr);
        Student s1=new Student(nameStr,genderStr,ageInt,markInt);
        stuList.add(s1);
    }
}